<?php

include_once("../dao/conexao_class.php");
include_once("../model/permissoes_model_class.php");

class PermissoesDAO {

    private $insert = "INSERT INTO permissoes(niv_id,opc_id) values(?,?);";
//    private $update = "UPDATE permissoes SET per_descricao = ? WHERE per_id = ?;";
    private $delete = "DELETE FROM permissoes WHERE niv_id = ?;";
    private $select = "SELECT * FROM permissoes;";
    private $select_by_nivel = "SELECT * FROM permissoes WHERE niv_id = ?;";
//    private $select_permissoes = "SELECT * FROM permissoes WHERE per_descricao = ?;";

    function insert($objPermissoes) {
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $ok = true;
//        var_dump($conn);
        //criando o preparedStatment
        if ($stm = $conn->prepare($this->insert)) {

            foreach($objPermissoes->getOpcoes() as $opc){
                
                $stm->bind_param("ii", $objPermissoes->getNivel(),$opc);
                if (!$stm->execute()) {
                
                    $conn->close();
                    $ok = false;
                    break;
                }
            }
            
            
        } 
        return $ok;
        
    }

    function update($objPermissoes) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("si", $objPermissoes->getDescricao(), $objPermissoes->getId());
//            $stm->bind_param("i",);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function delete($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function findAllPermissoes() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $perM = new PermissoesModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($nivel, $opcoes);


            //para cada resultado encontrado
            $i = 0;
            while ($stm->fetch()) {
                $perM = new PermissoesModel();
                
                $perM->setNivel($nivel);
                $perM->setOpcoes($opcoes);
                
                $lista[$i++] = $perM;
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findByNivel($nivel) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $perM = new PermissoesModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_nivel)) {
            
            $stm->bind_param('i', $nivel);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($niv, $opcoes);


            //para cada resultado encontrado
            $i = 0;
            while ($stm->fetch()) {
                $perM = new PermissoesModel();
                
                $perM->setNivel($niv);
                $perM->setOpcoes($opcoes);
                
                $lista[$i++] = $perM;
            }

            $conn->close();
            return $lista;
        }
    }
    

    function existe($permissoes) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select_permissoes)) {
            
            $stm->bind_param("s", $permissoes);
            
            $stm->execute();

            while ($stm->fetch()) {
                $conn->close();
                return true;
            }

        }
        
        $conn->close();
            return false;
    }
    
}

//$teste = new PermissoesDAO();
//$per = new PermissoesModel();
////$per->setId(3);
//$per->setDescricao("Administraçao");
//
////$per = 3;
//if($teste->insert($per)){
//    printf("Deletado com sucesso!");
//}else{
//    printf("Erro ao deletar!");
//}
//
//var_dump($teste->findAllPermissoes());

